<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link rel="stylesheet" href="../../../static/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="../../../static/css/public.css" media="all" />
</head>
<body class="childrenBody">
	<form class="layui-form" lay-filter="formTest" id="form">

		<!-- 基本信息模块 -->
		<fieldset class="layui-elem-field layui-field-title site-title">
			<legend>
				<div>基础信息</div>
			</legend>
		</fieldset>
		<table class="layui-table mag0">
			<colgroup>
				<col width="15%">
				<col width="30%">
				<col width="15%">
				<col width="30%">
			</colgroup>
			<tbody class="detailTable">
				<tr>
					<td class="detailTable_text">名称</td>
					<td><input type="text" class="layui-input " lay-verify="required" placeholder="请输入报销名称" name="name"></td>
					<td class="detailTable_text">金额</td>
					<td><input type="text" class="layui-input amount" disabled><input type="hidden" class="layui-input  amount" name="amount"></td>

				</tr>
				<tr>
					<td class="detailTable_text">原因</td>
					<td colspan="3"><input lay-verify="required" type="text" class="layui-input" placeholder="" name="reason"></input></td>

				</tr>
				<tr>
					<td class="detailTable_text">开始时间</td>
					<td><input type="text" name="sttm" id="sttm" lay-verify="required" autocomplete="off" class="layui-input">
					<td class="detailTable_text">结束时间</td>
					<td><input type="text" name="edtm" id="edtm" lay-verify="required" autocomplete="off" class="layui-input">
				</tr>
				<tr>
					<td class="detailTable_text">备注</td>
					<td colspan="3"><input type="text" class="layui-input" placeholder="" name="remarks"></input></td>
				</tr>
			</tbody>
		</table>
		<fieldset class="layui-elem-field layui-field-title site-title">
			<legend>
				<div>子项目</div>
			</legend>
		</fieldset>
		<table class="layui-table mag0" id="subreimburse" lay-filter="subreimburse">
			<thead>
				<tr>
					<th></th>
					<th>名称</th>
					<th>金额</th>
					<th>是否有发票</th>
					<th>备注</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td colspan="6">
						<div style="text-align: center">
							<a class="layui-btn layui-btn-xs" id="subreimburse-add">添加</a>
						</div>
					</td>
				</tr>
			</tbody>
		</table>
		<fieldset class="layui-elem-field layui-field-title site-title">
			<legend>
				<div>审核情况</div>
			</legend>
		</fieldset>
		<div class="layui-form-item layui-row layui-col-xs12">
			<label class="layui-form-label">审核原因</label>
			<div class="layui-input-block">
				<textarea placeholder="" class="layui-textarea" id="rwst" name="rwrs"></textarea>
			</div>
		</div>
		<div class="layui-form-item layui-row layui-col-xs12">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="yes">同意</button>
				<button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="no">不同意</button>
				<button type="button" class="layui-btn layui-btn-sm layui-btn-primary close-btn">取消</button>
			</div>
		</div>

	</form>
	<script type="text/javascript" src="../../../static/js/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../../static/layui/layui.js"></script>
	<script type="text/javascript" src="../../../static/js/index.js"></script>
	<script type="text/javascript" src="../../../static/custom/js/common.js"></script>
	<script type="text/javascript">
		layui.use([ 'form', 'laydate', 'layer' ], function() {
			var form = layui.form;
			var laydate = layui.laydate;
			var layer = parent.layer === undefined ? layui.layer : top.layer, $ = layui.jquery;
			var approveApi = "../../../finance/reimburse/update";
			var toUpdateApi = "../../../finance/reimburse/toUpdate";
			var insertApi = "../../../finance/reimburse/insert";
			var toUpdateDetailApi = "../../../finance/reimburse/toUpdateDetail";
			var updateDetailApi = "../../../finance/reimburse/updateDetail";
			var updateMethod = "put";
			var url = updateDetailApi;
			var method = updateMethod;
			//初始化日期
			laydate.render({
				elem : '#sttm'
			});
			//初始化日期
			laydate.render({
				elem : '#edtm'
			});
			//拼接子项目字段
			function subreimburseHtml(index, obj) {
				if (!obj) {
					obj = {
						name : '',
						amount : '',
						isreceipt : ''
					}
				}
				if (!obj.remarks) {
					obj.remarks = ''
				}
				if (!index) {
					index = 1;
				}
				var str = null;
				str += '<tr class="subreimburse">                                                                                                   ';
				str += '	<td class="detailTable_text subreimburse-text">报销项目' + index + '</td>                                                                               ';
				str += '	<td><input type="text" class="layui-input sname" lay-verify="required" placeholder="请输入项目名" name="sname" value="'+obj.name+'"></td>         ';
				str += '	<td><input type="text" class="layui-input samount" lay-verify="required/number" placeholder="请输入金额" name="samount" value="'+obj.amount+'"></td>         ';
				str += '	<td>                                                                                                                                      ';
				str += '		<select name="sisreceipt" class="sisreceipt" lay-verify="required">                                                         ';
				str += '			<option value="">是否有发票</option>                                                                                    ';
				str += '			<option value="1"' + (obj.isreceipt == 1 ? 'selected' : '') + '>是</option>                                                                                               ';
				str += '			<option value="2"' + (obj.isreceipt == 2 ? 'selected' : '') + ' >否</option>                                                                                               ';
				str += '		</select>                                                                                                                         ';
				str += '	</td>                                                                                                                                     ';
				str += '	<td><input type="text" class="layui-input sremarks"  name="sremarks" class="samount" value="'+obj.remarks+'"></td>         ';
				str += '	<td><a class="layui-btn layui-btn-xs layui-btn-danger deleteitem-btn style="display:none"">删除</a></td>';
				str += '</tr>';
				return str;
			}
			$("tbody").on("blur", ".samount", function() {
				var money = 0;
				for (var i = 0; i < $(".samount").length; i++) {
					money += Number($(".samount").eq(i).val());
				}
				$(".amount").val(money);
			});
			function setText() {
				for (var i = 1; i <= $(".subreimburse").length; i++) {
					$(".subreimburse-text").eq(i - 1).text("子项目" + i);
				}

			}
			//回显表单
			var id = getUrlParam("id");
			var type = getUrlParam("type");
			loadAjax( toUpdateDetailApi + "/" + id, "get", null, function(response) {
				baseCallBack(response, function(response) {
					fomrVal(form, "formTest", response.data.reimburse);
					$(".amount").val(response.data.reimburse.amount);
					var length = 0;
					for ( var i in response.data.subreimburse) {
						length++;
						$("#subreimburse-add").parent().parent().parent().before(subreimburseHtml(parseInt(i) + 1, response.data.subreimburse[i]));
					}
					$("input").attr("disabled", true);
					$("select").attr("disabled", true);
					$("textarea").attr("disabled", true);
					$("a").css("display", "none");
					$("#rwst").attr("disabled", false);
					$("#form").append("<input type='hidden' name='id' value='"+id+"' >");
					form.render();

				});
			});
			form.on("submit(yes)", function(data) {
				$("#form").append("<input type='hidden' name='rwst' value='"+2+"' >");
				for (var i = 0; i < $(".sremarks").length; i++) {
					if ($(".sremarks").eq(i).val() == '') {
						$(".sremarks").eq(i).val(" ");
					}
				}
				console.log($("#form").serialize());
				loadAjax( url, method, $("#form").serialize(), function(response) {
					baseCallBack(response, parentReloadCallBack);
				});
				return false;
			})
			form.on("submit(no)", function(data) {
				$("#form").append("<input type='hidden' name='rwst' value='"+3+"' >");
				for (var i = 0; i < $(".sremarks").length; i++) {
					if ($(".sremarks").eq(i).val() == '') {
						$(".sremarks").eq(i).val(" ");
					}
				}
				loadAjax( url, method, $("#form").serialize(), function(response) {
					baseCallBack(response, parentReloadCallBack);
				});
				return false;
			})
		})
	</script>
</body>
</html>